<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<!DOCTYPE html>

<html lang="zh-Hans">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<%@ include file="/common/common-css.jsp"%>
</head>
<style>
   .load_bg{background:#000;opacity:0.1;left:0px;top:0px;position:fixed;height:100%;width:100%;overflow:hidden;z-index:10000;}
   .load_img{left:50%;top:180px;position:fixed;height:50px;overflow:hidden;z-index:20000;}
</style>   
<body class="no-skin">

<div  class="load_bg" style="display:none;" ></div>
<img src="http://img.xmiles.cn/commentloading.gif" class="load_img" style="display:none"/>


<form id="queryForm">
	 <div class="widget-main">
		 <div class="row">
		
				 <div class="form-group col-sm-12 col-xs-12">
					<label class="col-sm-2 col-xs-2 control-label text-right"  for="soruceurl">链接:</label>
					<div class="input-group col-sm-10 col-xs-10">
						<input class="form-control" type="url" name="soruceurl" id="soruceurl" required="required"/>
					</div>
				 </div>
				 
				  <div class="form-group col-sm-12 col-xs-12">
					<label class="col-sm-2 col-xs-2 control-label text-right"  for="activity">项目:</label>
					<div class="input-group col-sm-10 col-xs-10">
						<input class="form-control" type="text" name="activity" id="activity"/>
					</div>
				 </div>
				 
				 <div class="form-group col-sm-12 col-xs-12">
					<label class="col-sm-2 col-xs-2 control-label text-right"  for="memo">备注:</label>
					<div class="input-group col-sm-10 col-xs-10">
						<input class="form-control" type="text" name="memo" id="memo" required="required"/>
					</div>
				 </div>
				 
				 <div class="form-group col-sm-12 col-xs-12">
					<label class="col-sm-2 col-xs-2 control-label text-right"  for="unit_price">单价:</label>
					<div class="input-group col-sm-10 col-xs-10">
						<input class="form-control" type="text" name="unit_price" id="unit_price" required="required"/>
					</div>
				 </div>
				 
				 <div class="form-group col-sm-12 col-xs-12">
					<label class="col-sm-2 col-xs-2 control-label text-right"  for="link_type">类型（cpc、cps、cpt、cpm等）:</label>
					<div class="input-group col-sm-10 col-xs-10">
						<input class="form-control" type="text" name="link_type" id="link_type" required="required"/>
					</div>
				 </div>

			 	<div class="form-group col-sm-12 col-xs-12">
					 <label class="col-sm-2 col-xs-2 control-label text-right"  for="environment">所属环境:</label>
					 <div class="input-group col-sm-10 col-xs-10">
						 <select class="form-control" name="environment" id="environment" required="required">
							 <option value="">请选择</option>
							 <option value="1">app内部</option>
							 <option value="0">app外部</option>
						 </select>
				 </div>

			 </div>
		 </div>
		 <div class="text-center">
		 	  <button class="btn  btn-success no-border" name="btn-save" id="btn-save">
						<i class="ace-icon fa fa-floppy-o bigger-160"></i> 添加
				</button>
		 </div>
	 </div>	
</form>
<%@ include file="/common/common-js.jsp" %>
<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
<script type="text/javascript" src="${ctx}/js/jquery.form.js"></script>
<script>      
		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
 			var r = window.location.search.substr(1).match(reg);
			if (r != null) return decodeURI(r[2]); return null;
	 	};
	 	
		jQuery(function() {
			
			$("#btn-save").bind("click",function() {
				
				var soruceurl = $('#soruceurl').val();
				var memo = $('#memo').val();
				var activity = $('#activity').val();
				var unit_price = $('#unit_price').val();
				var link_type = $('#link_type').val();
				var environment = $('#environment').val();

				if(environment == ""){
                    parent.bootbox.alert("请选择链接所属环境");
				    return;
				}
		
				if(isNaN(unit_price)){
					parent.bootbox.alert("单价只能为数字");
					return;
				}
				if(soruceurl == '' || soruceurl == null){
					$('#soruceurl').focus();
				}else if(memo == '' || memo == null){
					$('#memo').focus();
				}else if(unit_price == '' || unit_price == null){
					$('#unit_price').focus();
				}else if(link_type == '' || link_type == null){
					$('#link_type').focus();
				}else{
					$.ajax({
						"type": "POST",
						"dataType": 'json',
						"url": "${ctx}/spread/addShortLinkRequest.action",
						"data":{"soruceurl": soruceurl, "memo": memo,"unit_price":unit_price,"link_type":link_type,"activity":activity, "environment":environment},
						"beforeSend": function(){
							$(".load_bg,.load_img").show();
						},
						"success": function(resp){	
	                        console.log(resp);					
							if(resp.result.status == 1){
								parent.parent['__extdialog_active'].dialog('close');
								parent.main_frame.query_any();	
							}
						},
						"error": function(XMLHttpRequest, textStatus, errorThrown) {
	                        alert(textStatus);
						},
						"complete": function(){
							$(".load_bg,.load_img").hide();
						}
					});
				};
				return false;
			});

            /**
			 * 为soruceurl输入框添加一个失去焦点事件
			 * 将输入框中的文本进行替换，将http转为https
			 *
			 * created by chuIllusions_tan 20180528
             */
            $("#soruceurl").blur(function () {
				//1.获取文本内容
				var url = $("#soruceurl").val();
				//2.使用正则表达式匹配是否以https开头，或是http开头
                var strRegex = "^((https|http)?://)";
                var re= new RegExp(strRegex);
                var tuanzi = "tuanzidai.cn";
                if ( (url.indexOf(tuanzi)!=-1) && re.test(url)){
					//是否需要将http转为https
                    strRegex = "^(http://)";
                    re = new RegExp(strRegex);
                    if (re.test(url)){
                        $("#soruceurl").attr("disabled","disabled");
                        //替换、回显、提示
						url = url.replace("http","https");
                        $("#soruceurl").val("");
                        $("#soruceurl").attr("placeholder","正在从http转换到https");
						setTimeout(function () {
                            $("#soruceurl").val(url);
                            $("#soruceurl").removeAttr("disabled")
                        },1000)
					}
				}
            });
	  });
	</script>
</body>
</html>